package com.topgay.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.topgay.domain.CSCenter;
import com.topgay.domain.Member;
import com.topgay.domain.ValueStrings;
import com.topgay.service.CSCenterService;
import com.topgay.service.MemberService;

@Controller
@RequestMapping("/admin/")
public class AdminController {

	@Autowired
	private MemberService service;
	
	@Autowired
	private CSCenterService cscenterService;
	
	@RequestMapping("/login/main.do")
	public String mainPage(){
		return "admin/main";
	}
	
	//로그인
	@RequestMapping(value="/login.do", method=RequestMethod.GET)
	public String loginForm(){
		return "/admin/body/login.jsp";
	}
	
	//로그인 처리
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	public String login(String id, String password, HttpSession session, ModelMap map) throws SQLException{
		Member member = service.getMemberById(id);
		
		if(member!=null){
			if(password!=null && password.trim().length()!=0){
				if(password.equals(member.getPassword())){
					//로그인 성공
//					System.out.println(member);
					session.setAttribute("login_admin_info", member);
					return "admin/main";
				}else{
					//로그인 실패 : 비번 일치안함
					map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPw());
				}
			} else {
				//비밀번호 틀린 경우
				map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginPwEmpty());
			}
		} else {
			//ID가 틀린경우, DB에 회원정보값 없음
			map.addAttribute("error_msg", ValueStrings.getInstance().getErrorMsgLoginId());
			
		}
		return "/admin/body/login.jsp";
	}
	
	@RequestMapping(value="/logout.do")
	public String logout(HttpSession session) throws IOException{
		session.invalidate();
//		return "admin/main";
		return "redirect:/admin/logout_refresh.html";
	}
	
	//회원정보
	@RequestMapping("/login/member_info.do")
	public String memberInfo(){
		return "admin/member_info";
	}
	
	//관리자정보
	@RequestMapping("/login/member_admin_info.do")
	public String adminInfo(){
		return "admin/member_admin_info";
	}
	
	//CS Center - 공지사항
	@RequestMapping("/login/cscenter_notice.do")
	public String cscenterNotice(@RequestParam(defaultValue="1")String page, ModelMap model) throws Exception{
		int pageNo = 1;
		try {
			pageNo = Integer.parseInt(page);
		}catch(Exception e){
			pageNo = 1;
		}
		
//		return "admin/cscenter_notice";
		return cscenterList(pageNo, 5, model, "admin/cscenter_notice");
	}
	
	//CS Center - FAQ
	@RequestMapping("/login/cscenter_faq.do")
	public String cscenterFaq(@RequestParam(defaultValue="1")String page, ModelMap model) throws Exception{
		int pageNo = 1;
		try {
			pageNo = Integer.parseInt(page);
		}catch(Exception e){
			pageNo = 1;
		}
//		return "admin/cscenter_faq";
		return cscenterList(pageNo, 6, model, "admin/cscenter_faq");
		
	}
	
	/**
	 * Notice, FAQ 목록 조회
	 * @param page
	 * @param divide - 5:Notice, 6:FAQ
	 * @param model
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public String cscenterList(int page, int divide, ModelMap model, String url) throws Exception{
		Map map = cscenterService.getCSCenterList(page, divide);
		model.addAllAttributes(map);
		return url;
	}

	/**
	 * 공지사항 - 글쓰기 양식
	 * @return
	 */
	@RequestMapping("/login/cscenter_write_form.do")
	public String cscenterWriteForm(){
		return "admin/cscenter_write_form";
	}
	
	/**
	 * 공지사항 - 글쓰기 양식
	 * @return
	 */
	@RequestMapping("/login/cscenter_write_content.do")
	public ModelAndView cscenterWriteContent(CSCenter cscenter, int divide, HttpSession session){
		
		Member member = (Member)session.getAttribute("login_admin_info");
		cscenter.setId(member.getId());
		cscenter.setNickname(member.getNickname());
		cscenter.setEmail(member.getEmail());
		cscenter.setDivide(divide);
		
//		System.out.println(cscenter.toString());
		
//		2. Business Logic - Model(Business Service)로 요청
		cscenterService.getWriteContent(cscenter);
		
		return new ModelAndView("/admin/login/getContent.do?no="+cscenter.getCscenterNo());
	}
	
	@RequestMapping("/login/getContent.do")
	public String getCSCenterContent(int no, ModelMap map){
		String url = null;
		
		try {
			//2. Business Logic 요청 - Model
			CSCenter cscenter = cscenterService.getCSCenterContentByNo(no);
			map.addAttribute("cscenter", cscenter);
			url = "admin/cscenter_show_content";
		}catch(Exception e){
			e.printStackTrace();
			map.addAttribute("error_msg", "글 조회도중 오류 발생 : "+e.getMessage());
			url = "redirect:/admin/error.jsp";
		}
		return url;
	}
	
	
	//CS Center - 1:1문의
	@RequestMapping("/login/cscenter_qna.do")
	public String cscenterQna(){
		return "admin/cscenter_qna";
	}

	//CS Center - 신고하기
	@RequestMapping("/login/cscenter_declare.do")
	public String cscenterDeclare(){
		return "admin/cscenter_declare";
	}

	//게시판 - 자유게시판
	@RequestMapping("/login/community_free.do")
	public String communityFree(){
		return "admin/community_free";
	}

	//게시판 - 팁게시판
	@RequestMapping("/login/community_tip.do")
	public String communityTip(){
		return "admin/community_tip";
	}

	//게시판 - 핫이슈
	@RequestMapping("/login/community_news.do")
	public String communityNews(){
		return "admin/community_news";
	}

	//게시판 - 고충상담
	@RequestMapping("/login/community_counseling.do")
	public String communityCounseling(){
		return "admin/community_counseling";
	}

}
